using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ServidorCatalogo : System.Web.Services.WebService
{
    public bool blnInternal = false;
    public ServidorCatalogo()
    {

    }
    public ServidorCatalogo(bool Internal)
    {
        blnInternal = Internal;
    }
    [WebMethod]
    public string QueryBySubject(String Subject, string MT)
    {
        Cola clsCola = new Cola();
        if (!blnInternal)
        {
            clsCola.AgregarPedido(this.GetHashCode(), DateTime.Parse(MT));
        }

        System.Xml.XmlDocument ResultXml = new System.Xml.XmlDocument();
        ResultXml.LoadXml("<RESULT/>");
        string path = Util.GetConfig<string>("DataBase");
        path = Server.MapPath(path);
        Base clsBase = new Base(path);
        int j = 0;
        try
        {
            for (j = 0; j < 4; j++)
            {
                if (clsBase.Lista[j, 3].Trim().ToUpper() == Subject.Trim().ToUpper())
                {
                    System.Xml.XmlNode Nodo = ResultXml.CreateNode(System.Xml.XmlNodeType.Element, "Item", String.Empty);
                    Nodo.Attributes.Append(ResultXml.CreateAttribute(string.Empty, "Title", string.Empty));
                    Nodo.Attributes["Title"].Value = clsBase.Lista[j, 0];
                    Nodo.Attributes.Append(ResultXml.CreateAttribute("Stock"));
                    Nodo.Attributes["Stock"].Value = clsBase.Lista[j, 1];
                    Nodo.Attributes.Append(ResultXml.CreateAttribute("Price"));
                    Nodo.Attributes["Price"].Value = clsBase.Lista[j, 2];
                    Nodo.Attributes.Append(ResultXml.CreateAttribute("Subject"));
                    Nodo.Attributes["Subject"].Value = clsBase.Lista[j, 3];
                    ResultXml.DocumentElement.AppendChild(Nodo);
                }

            }
            if (!blnInternal)
            {
                clsCola.RemoverItem();
            }
            return ResultXml.InnerXml;
        }
        catch (Exception e)
        {
            if (!blnInternal)
            {
                clsCola.RemoverItem();
            }
            return (e.Message);
        }
    }

    [WebMethod]
    public string QueryByItem(String Item, String MT)
    {
        Cola clsCola = new Cola();
        if (!blnInternal)
        {
            clsCola.AgregarPedido(this.GetHashCode(), DateTime.Parse(MT));
        } 
        
        System.Xml.XmlDocument ResultXml = new System.Xml.XmlDocument();
        ResultXml.LoadXml("<RESULT/>");

        string path = Util.GetConfig<string>("DataBase");
        path = Server.MapPath(path);
        Base clsBase = new Base(path);
        int j = 0;
        try
        {
            for (j = 0; j < 4; j++)
            {
                if (clsBase.Lista[j, 4].Trim().ToUpper() == Item.Trim().ToUpper())
                {
                    System.Xml.XmlNode Nodo = ResultXml.CreateNode(System.Xml.XmlNodeType.Element, "Item", String.Empty);
                    Nodo.Attributes.Append(ResultXml.CreateAttribute(string.Empty, "Title", string.Empty));
                    Nodo.Attributes["Title"].Value = clsBase.Lista[j, 0];
                    Nodo.Attributes.Append(ResultXml.CreateAttribute("Stock"));
                    Nodo.Attributes["Stock"].Value = clsBase.Lista[j, 1];
                    Nodo.Attributes.Append(ResultXml.CreateAttribute("Price"));
                    Nodo.Attributes["Price"].Value = clsBase.Lista[j, 2];
                    Nodo.Attributes.Append(ResultXml.CreateAttribute("Subject"));
                    Nodo.Attributes["Subject"].Value = clsBase.Lista[j, 3];
					Nodo.Attributes.Append(ResultXml.CreateAttribute("Id"));
					Nodo.Attributes["Id"].Value = clsBase.Lista[j, 4];
                    ResultXml.DocumentElement.AppendChild(Nodo);
                }

            }
            if (!blnInternal)
            {
                clsCola.RemoverItem();
            }
            return ResultXml.InnerXml;
        }
        catch (Exception e)
        {
            if (!blnInternal)
            {
                clsCola.RemoverItem();
            }
            return (e.Message);
        }
    }

    [WebMethod]
    public string Update(String Item, String Stock, String Price, String MT)
    {
        Cola clsCola = new Cola();
        if (!blnInternal)
        {
            clsCola.AgregarPedido(this.GetHashCode(), DateTime.Parse(MT));
        }

        System.Xml.XmlDocument ResultXml = new System.Xml.XmlDocument();
        ResultXml.LoadXml("<RESULT/>");
        string path = Util.GetConfig<string>("DataBase");
        path = Server.MapPath(path);
        Base clsBase = new Base(path);
        int j = 0;
        Boolean HayItem = false;
        try
        {
            for (j = 0; j < 4; j++)
            {
                if (clsBase.Lista[j, 4].Trim().ToUpper() == Item.Trim().ToUpper())
                {
                    clsBase.Lista[j, 1] = Stock;
                    clsBase.Lista[j, 2] = Price;
                    HayItem = true;
                }

            }

            if (HayItem)
            {
                clsBase.SaveToFile();
                if (!blnInternal)
                {
                    clsCola.RemoverItem();
                }
                return "Item actualizado!";
            }
            else
            {
                if (!blnInternal)
                {
                    clsCola.RemoverItem();
                }
                return "Item no existente";
            }
        }
        catch (Exception e)
        {
            if (!blnInternal)
            {
                clsCola.RemoverItem();
            }
            return (e.Message);
        }
    }
}
